package com.android.happymall;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.happymall.utils.ConstantsUrl;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 引导页
 */

public class GuideActivity extends AppCompatActivity implements View.OnClickListener {

    @BindView(R.id.viewpager)
    ViewPager viewpager;

    private List<View> viewList;
    private int[] imgResArr = new int[]{R.mipmap.guide_1, R.mipmap.guide_2};

    public static void start(Context context) {
        Intent intent = new Intent(context, GuideActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_guide);
        ButterKnife.bind(this);
        initData();
        initView();
        super.onCreate(savedInstanceState);
    }

    public void initData() {
        viewList = new ArrayList<View>(imgResArr.length);
        for (int i = 0; i < imgResArr.length; i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.activity_guide_item, null);
            view.setBackgroundResource(R.color.white);
            ImageView imgView = (ImageView) view.findViewById(R.id.guide_image);
            imgView.setBackgroundResource(imgResArr[i]);
            viewList.add(view);
            imgView.setOnClickListener(this);
        }
    }

    public void initView() {
        viewpager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return viewList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(viewList.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(viewList.get(position));
                return viewList.get(position);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.guide_image:
                if (viewpager.getCurrentItem() == (imgResArr.length - 1)) {
                    SharedPreferences myPref = getSharedPreferences(ConstantsUrl.SHAREDPREFERENCES_NAME, 0);
                    SharedPreferences.Editor editor = myPref.edit();
                    editor.putBoolean(ConstantsUrl.CACHE_HOME_GUIDE, true);
                    editor.commit();
                    startActivity(new Intent(this, MainActivity.class));
                    finish();
                }
                break;
        }
    }
}
